package com.zmn.oms.admin.controller.conf.workturn.servcategory;

import com.zmn.common.dto.KvDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.model.entity.conf.workturn.servcategory.PlatServCategory;
import com.zmn.oms.model.entity.conf.workturn.servcategory.log.PlatServCategoryLog;
import com.zmn.oms.model.entity.conf.workturn.servcategory.log.PlatServCategoryLogQuery;
import com.zmn.oms.services.interfaces.conf.workturn.servcategory.PlatServCategoryService;
import com.zmn.oms.services.interfaces.conf.workturn.servcategory.log.PlatServcategoryLogService;
import com.zmn.base.plat.engine.common.enums.PlatEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/conf/workturn/servcategory/")
public class PlatServCategoryListController extends PlatServCategoryBaseController {

    @Autowired
    private PlatServCategoryService platServCategoryService;
    @Autowired
    private PlatServcategoryLogService logService;

    /**
     * 开通分类
     *
     * @param plat 平台
     * @return the model and view
     * @author linfeng
     * @since 2020/04/13
     */
    @RequestMapping(value = "detail")
    //@RequiresPermissions("/plat/serv/category/servCategory.action")
    @SuppressWarnings("Duplicates")
    public ModelAndView servCategory(Integer plat) {
        log.info("servCategory-request-0-->{}", plat);
        ModelAndView mav = new ModelAndView("conf/workturn/servcategory/platServCategory");
        if (plat == null || plat == 0) {
            plat = PlatEnum.ZMN.value;
        }

        // 根据开通权限过滤默认展示的tab
        plat = getRealShowPlat(plat);

        List<KvDTO<Integer, String>> servCategorys = Collections.emptyList();
        List<PlatServCategoryLog> logList = new ArrayList<>();
        if (!NumberUtil.isNullOrZero(plat)) {
            log.info("servCategory-request-1-->{}", plat);
            servCategorys = super.getAllChildCategory(1);
            log.info("servCategory-request-2-->{}", plat);
            // 只显示平台开通分类最近50条操作日志
            PlatServCategoryLogQuery logQuery = new PlatServCategoryLogQuery();
            logQuery.setPlat(plat);
            logQuery.setPageIndex(0);
            logQuery.setPageSize(10);
            logList = logService.listPageByQuery(logQuery);
            log.info("servCategory-request-3-->{}", plat);
        }

        List<PlatServCategory> serviceCategoryList = platServCategoryService.listByPlat(plat);
        log.info("servCategory-request-4-->{}", plat);
        List<String> categIds = serviceCategoryList.stream().map(p -> p.getServCategId() + "-" + p.getCategOneId() + "-" + p.getCategId())
                .collect(Collectors.toList());
        if (servCategorys != null && servCategorys.size() > 0) {
            mav.addObject("currentServCategId", servCategorys.get(0).getKey());
        }

        mav.addObject("categIds", categIds);
        mav.addObject("servCategorys", servCategorys);
        mav.addObject("plat", plat);
        mav.addObject("items", logList);

        log.info("servCategory-response->{}", plat);
        return mav;
    }
}
